﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using PGI.EN.Inventario;

namespace PGI.AD.Inventario
{
    public class MovimientoInventarioAd
    {
        public static int InsertarMovimiento(MovimientoInventarioEn mov)
        {
            SqlCommand command = null;
            SqlConnection connection = null;
            SqlDataReader reader = null;

            string sql = "TrnMovInventarioI";

            try
            {
                connection = DataBaseFactory.SqlOpenDatabase("PGI");

                command = new SqlCommand(sql, connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@ProductoIDMov", mov.ProductoID);
                command.Parameters.AddWithValue("@UsuarioID", mov.UsuarioID);
                command.Parameters.AddWithValue("@TipoMovimiento", mov.TipoMovimiento);
                command.Parameters.AddWithValue("@Cantidad", mov.Cantidad);
                command.Parameters.AddWithValue("@CostoUnitario", mov.CostoUnitario);
                command.Parameters.AddWithValue("@PrecioVenta", mov.PrecioVenta);
                command.Parameters.AddWithValue("@TallaID", mov.TallaID);
                command.Parameters.AddWithValue("@ColorID", mov.ColorID);
                command.Parameters.AddWithValue("@MaterialID", mov.MaterialID);
                command.Parameters.AddWithValue("@TemporadaID", mov.TemporadaID);

                command.Parameters.Add("@Resultado", SqlDbType.Int).Direction = ParameterDirection.Output;

                command.ExecuteNonQuery();

                int MovimientoID = Convert.ToInt32(command.Parameters["@Resultado"].Value);


                return MovimientoID;
            }

            catch (SqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                if (reader != null)
                    reader.Close();
                DataBaseFactory.SqlCloseDatabase("PGI");
            }
        }
        public static int InsertarMovimiento_OpcProducto(int MovimientoID, int OpcionProdID)
        {
            SqlCommand command = null;
            SqlConnection connection = null;
            SqlDataReader reader = null;

            string sql = "TrnMovInventarioxOpcProductoI";

            try
            {
                connection = DataBaseFactory.SqlOpenDatabase("PGI");

                command = new SqlCommand(sql, connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.AddWithValue("@MovimientoID", MovimientoID);
                command.Parameters.AddWithValue("@OpcProdID", OpcionProdID);

                command.Parameters.Add("@Resultado", SqlDbType.Int).Direction = ParameterDirection.Output;

                command.ExecuteNonQuery();

                int Resultado = Convert.ToInt32(command.Parameters["@Resultado"].Value);


                return Resultado;
            }

            catch (SqlException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

            finally
            {
                if (reader != null)
                    reader.Close();
                DataBaseFactory.SqlCloseDatabase("PGI");
            }
        }
    }
}
